package com.tanhua.service;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.tanhua.dao.AnalysisByDayMapper;
import com.tanhua.dao.LogMapper;
import com.tanhua.domain.db.Year;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.domain.vo.YearVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tanhua.utils.ComputeUtil.computeRate;

@Service
public class DashboardService {

    @Autowired
    private LogMapper logMapper;
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;


    /**
     * 概要信息
     *
     * @return
     */
    public ResponseEntity summary() {
        AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();

        //累计用户
        Long cumulativeUsers = analysisByDayMapper.countNumRegistered();
        summaryVo.setCumulativeUsers(cumulativeUsers);

        //今天时间
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        //今日新增用户
        Integer newUsersToday = logMapper.findNumLogin(format, "0102");
        summaryVo.setNewUsersToday(newUsersToday.longValue());

        //今日活跃用户
        Integer activeUsersToday = logMapper.findNumActive(format);
        summaryVo.setActiveUsersToday(activeUsersToday.longValue());

        //今日登录次数
        Integer loginTimesToday = logMapper.findNumLogin(format, "0101");
        summaryVo.setLoginTimesToday(loginTimesToday.longValue());

        //前7天日期
        String aWeekAgo = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -7).toString("yyyy-MM-dd");
        //过去7天的活跃用户
        Integer activePassWeek=analysisByDayMapper.findActivePassWeek(aWeekAgo,format);
        summaryVo.setActivePassWeek(activePassWeek.longValue());

        //上一个月
        String lastMonth = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -30).toString("yyyy-MM-dd");
        //过去30天的活跃用户
        Integer activePassMonth = analysisByDayMapper.findActivePassMonth(lastMonth, format);
        summaryVo.setActivePassMonth(activePassMonth.longValue());

        //昨天日期
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");  //昨日时间
        //今日新增用户涨跌率
        Integer newUsersTodayRate = logMapper.findNumLogin(yesterday, "0102"); //昨日新增用户
        BigDecimal newChainRatio = computeRate(newUsersToday, newUsersTodayRate);
        summaryVo.setNewUsersTodayRate(newChainRatio);

        //今日登录次数涨跌率
        Integer loginTimesTodayRate = logMapper.findNumLogin(yesterday, "0101");
        BigDecimal loginChainRatio = computeRate(loginTimesToday, loginTimesTodayRate);
        summaryVo.setLoginTimesTodayRate(loginChainRatio);

        //今日活跃用户涨跌率
        Integer activeUsersTodayRate = logMapper.findNumActive(yesterday);//昨日活跃度
        BigDecimal activeChainRatio = computeRate(activeUsersToday, activeUsersTodayRate);
        summaryVo.setActiveUsersTodayRate(activeChainRatio);

        return ResponseEntity.ok(summaryVo);
    }

    /**
     * 新增、活跃用户、次日留存率
     *
     * @param sd   开始时间
     * @param ed   结束时间
     * @param type 属性
     * @return
     */
    public ResponseEntity findUsers(Long sd, Long ed, String type) {
        //数据暂不处理
        String edTime = DateUtil.offset(new Date(ed), DateField.DAY_OF_MONTH, 1).toString("yyyy-MM-dd");

        YearVo yearVo = new YearVo();
        //皆为必传参数,不做判断   0101为登录，0102为注册
        switch (type) {
            //新增用户
            case "101": {
                //本年
                List<Year> thisYear = new ArrayList<>();
                int i =0;   // 自增量
                while (true){
                    //起始时间
                    String everyDaySd = DateUtil.offset(new Date(sd), DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    if (edTime.equals(everyDaySd)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumLogin(everyDaySd, "0102");
                    // 封装vo数据
                    Year year = new Year();
                    year.setTitle(everyDaySd);
                    year.setAmount(numLogin);
                    thisYear.add(year);
                    i++;
                }

                //去年
                List<Year> lastYear = new ArrayList<>();
                i =0;   //清空i的自增量
                while (true){
                    //String string = DateUtil.offsetMonth(new Date(sd), -12).toString("yyyy-MM-dd");
                    String everyDaySd = DateUtil.offsetMonth(new Date(sd), -12).offset(DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    String lastEdTime = DateUtil.offsetMonth(new Date(ed), -12).offset(DateField.DAY_OF_MONTH, 1).toString("yyyy-MM-dd");
                    if (lastEdTime.equals(everyDaySd)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumLogin(everyDaySd, "0102");
                    // 封装数据
                    Year year = new Year();
                    year.setTitle(everyDaySd);
                    year.setAmount(numLogin);
                    lastYear.add(year);
                    i++;
                }
                yearVo.setThisYear(thisYear);
                yearVo.setLastYear(lastYear);

                break;
            }
            //活跃用户
            case "102": {
                //本年
                List<Year> thisYear = new ArrayList<>();
                int i =0;
                while (true){
                    //起始时间
                    String everyDaySd = DateUtil.offset(new Date(sd), DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    if (edTime.equals(everyDaySd)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumActive(everyDaySd);
                    // 封装数据
                    Year year = new Year();
                    year.setTitle(everyDaySd);
                    year.setAmount(numLogin);
                    thisYear.add(year);
                    i++;
                }

                //次年
                List<Year> lastYear = new ArrayList<>();
                i =0;   //清空i的自增量
                while (true){
                    String everyDaySd = DateUtil.offsetMonth(new Date(sd), -12).offset(DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    String edTime1 = DateUtil.offsetMonth(new Date(ed), -12).offset(DateField.DAY_OF_MONTH, 1).toString("yyyy-MM-dd");
                    if (edTime1.equals(everyDaySd)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumActive(everyDaySd);
                    // 封装list数据
                    Year year = new Year();
                    year.setTitle(everyDaySd);
                    year.setAmount(numLogin);
                    lastYear.add(year);
                    i++;
                }
                // 封装vo数据
                yearVo.setThisYear(thisYear);
                yearVo.setLastYear(lastYear);
                break;
            }
            //每天次日留存用户
            default: {
                //本年
                List<Year> thisYear = new ArrayList<>();
                int i =0;
                while (true){
                    //终止时间--当等于结束时间时终止查询
                    String everyDayUp = DateUtil.offset(new Date(sd), DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    //起始时间
                    String everyDaySd = DateUtil.offset(new Date(sd), DateField.DAY_OF_MONTH, -1 + i).toString("yyyy-MM-dd");
                    //判断是否日期相同
                    if (edTime.equals(everyDayUp)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumRetention1d(everyDaySd, everyDayUp);
                    // TODO: 2021/9/4 封装vo数据
                    Year year = new Year();
                    year.setTitle(everyDayUp);
                    year.setAmount(numLogin);
                    thisYear.add(year);
                    i++;
                }

                //次年
                i =0;
                List<Year> lastYear = new ArrayList<>();
                while (true){
                    //终止时间--当等于结束时间时终止查询
                    String everyDayUp = DateUtil.offsetMonth(new Date(sd), -12).offset(DateField.DAY_OF_MONTH, i).toString("yyyy-MM-dd");
                    //起始时间
                    String everyDaySd = DateUtil.offset(new Date(sd), DateField.DAY_OF_MONTH, -1 + i).toString("yyyy-MM-dd");

                    String edTime1 = DateUtil.offsetMonth(new Date(ed), -12).offset(DateField.DAY_OF_MONTH, 1).toString("yyyy-MM-dd");
                    //判断是否日期相同
                    if (edTime1.equals(everyDayUp)) {
                        break;
                    }
                    Integer numLogin = logMapper.findNumRetention1d(everyDaySd, everyDayUp);
                    // TODO: 2021/9/4 封装vo数据
                    Year year = new Year();
                    year.setTitle(everyDayUp);
                    year.setAmount(numLogin);
                    lastYear.add(year);
                    i++;
                }
                yearVo.setThisYear(thisYear);
                yearVo.setLastYear(lastYear);
                break;
            }
        }
        return ResponseEntity.ok(yearVo);
    }
}
